﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using wUniApp.MVC.App_Data;

//
// http://www.singingeels.com/Articles/Self_Sorting_GridView_with_LINQ_Expression_Trees.aspx

namespace wUniApp.MVC.Controllers
{
  public class CustomersController : wBaseController<Customer>
  {
    public override ActionResult Index()
    {
      var customers = dbc.Customers.Include(c => c.Product);
      return View(customers.ToList());
    }

    public ActionResult IndexSort(string ColumnName, string Filter = null)
    {
      var session = System.Web.HttpContext.Current.Session;
      string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();
      string CSO = controllerName + ".SortingOrder";
      
      session[CSO] = String.IsNullOrEmpty(session[CSO] as string) ? "Desc" : "";
      session[controllerName + ".ColumnName"] = ColumnName;

      var items = from stu in dbc.GetDbSet() select stu;

      // Sorting
      if (!String.IsNullOrEmpty(ColumnName))
      {
        var param = Expression.Parameter(typeof(Customer), "Customer");
        var expC = Expression.Lambda<Func<Customer, object>>(Expression.Property(param, ColumnName), param);

        switch ((string)session[CSO])
        {
          case "":
            items = items.OrderBy(expC);
            break;
          case "Desc":
            items = items.OrderByDescending(expC);
            break;
        }
      }

      // Filtering
      if (!String.IsNullOrEmpty(Filter))
      {
        items = items.Where(c => c.LastName.ToLower().Contains(Filter.Trim().ToLower()));
      }
      return View("Index", items.ToList());
    }

    // GET: Customers/Create
    public override ActionResult Create()
    {
      ViewBag.ProductId = new SelectList(dbc.Products, "Id", "Name");
      return base.Create();
    }

    // POST: Customers/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public override ActionResult Create([Bind(Include = "Id,FirstName,LastName,Age,MonthlyIncome,ChildsCount,ProductId,Notes,Email")] Customer customer)
    {
      return base.Create(customer);
    }

    // GET: Customers/Edit/5
    public override ActionResult Edit(int? id)
    {
      if (id == null)
      {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      Customer customer = dbc.Customers.Find(id);
      if (customer == null)
      {
        return HttpNotFound();
      }
      ViewBag.ProductId = new SelectList(dbc.Products, "Id", "Name", customer.ProductId);
      return View(customer);
    }

    // POST: Customers/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public override ActionResult Edit([Bind(Include = "Id,FirstName,LastName,Age,MonthlyIncome,ChildsCount,ProductId,Notes,Email")] Customer customer)
    {
      if (ModelState.IsValid)
      {
        dbc.Entry(customer).State = EntityState.Modified;
        dbc.SaveChanges();
        return RedirectToAction("Index");
      }
      ViewBag.ProductId = new SelectList(dbc.Products, "Id", "Name", customer.ProductId);
      return View(customer);
    }
  }
}
